package util.webSearch;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.net.URLEncoder;

import java.util.HashSet;
import java.util.LinkedList;


import util.io.FileInput;
import util.io.URLInput;

import util.parser.ParseBingWebResults;



public class BingQuery implements Constants {

	/**
	 * @param args
	 */
	
	
	private String chooseID(){
		
		
	
		
		int MAX = Constants.BING_ID.length - 1;
		int index = (int) Math.round(((double) Math.random() * (MAX)));
		
		return Constants.BING_ID[index];
		
	}
	
	public  LinkedList<WebResult> bingQuery(String query, int max_results, String path) throws IOException, URISyntaxException {

		LinkedList<WebResult> res = new LinkedList<WebResult>();
		
			String post = null;
		

			
			String id= chooseID();
			
			String url = "http://api.bing.net/json.aspx?AppId="+id+"&Version=2.2&Market=en-US&Query="
			+URLEncoder.encode(query, "utf8")+
			"&Sources=web+spell&Web.Count="+max_results+"&JsonType=raw";
			

			post = URLInput.submitURLRequestUDF(url, path);
			
						
			if(post == null  || (post.equals("")) ){
				
				
				return null;
			}
			
			res = ParseBingWebResults.results(query, post);
			
			
		

		return res;

	}
	
	
	public static HashSet<String> getStrings(String path){
		
		HashSet<String> previous_queries= new HashSet<String>();
		
		File f=  new File(path);
		
		if(!f.isFile()) return previous_queries;
		
		FileInput in = new FileInput(path);
		String line = in.readString();
		while(line!=null){
			
			
			
			String t[] = line.trim().split("\t");
			
			previous_queries.add(t[0]);
			line= in.readString();
		}
		
		
	
		return previous_queries;
	}
	
	public static void main(String[] args) throws IOException, URISyntaxException  {
	
		
		
		String path =	"/dictionary/user_agent_list.txt";
		if (args.length != 3) {

			System.out.println("Usage: input_file previous_results delay");
			System.exit(1);
		}
		
		Integer delay = Integer.valueOf(args[2]);
		HashSet<String> previous_queries= getStrings(args[1]);
		FileInput in = new FileInput(args[0]);

		BingQueryJson q = new BingQueryJson();
		String line = in.readString();
		
		while (line != null) {
			String t[] = line.split("\t");
			String query = t[6];
			if(previous_queries.contains(query)){
				
				line= in.readString();
				continue;
			}
			LinkedList<WebResult> list = q.bingQuery(query, 15,path);
			
			try {
				Thread.sleep(delay);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new IOException();
			}
			
			
			for (int i = 0; i < list.size(); i++) {
				WebResult result = list.get(i);
				String ou= query.trim().replaceAll("\t"," ")+ "\t"+ result.getUrl().trim().replaceAll("\t"," ")+ "\t"+ result.getTitle().trim().replaceAll("\\s+"," ")+ "\t"+ result.getSnippet().trim().replaceAll("\\s+"," ")+ "\t" + (i+1) ;
				
				PrintStream out = new PrintStream(System.out, true, "UTF-8");
			    out.println(ou);
			//	System.out.println(ou);

			}

			line = in.readString();
		}
		
	

	}
}